$(function () {
    getHotComment();
    getNewetsComment(1);

    $("#messageCon").on("input propertychange",function (event) {
        $("#messageCon .length").html($("#messageCon").val().length);
    });

    $("#rMessage").on("input propertychange",function (event) {
        $("#rMessage .length").html($("#rMessage").val().length);
    });

});

//监听评论输入的字数
function limitTextarea(self,nowLength) {
    $(self).on("input propertychange",function (event) {
        var content = $(self).val();
        if(content.length >= 99){
            $(self).val(content.substring(0,99));
        }
        $(nowLength).html(content.length);
    });
    $(self).blur(function () {
        $(self).off("input propertychange");
    });
}

//提交评论信息
function commitComment(cId,msg,isReply) {
        isLogin();
        $("#messageCon").val('');
        $("#messageCon").blur();
        var uName = $("#uName").val();
        var uId = $("#uId").val();
        var mId = $("#musicRid").val();
        $.ajax({
            url: "/comment/insertComment",
            type: "post",
            data: {cId: cId, uName: uName, msg: msg, uId: uId, mId: mId, isReply: isReply},
            success: function (result) {
                if (result.commitResult = "success") {
                    $("#commentSuccess").animate({opacity: "1"}, 1000);
                    $("#commentSuccess").animate({opacity: "0"}, 1000);
                    getNewetsComment(1);
                    $("#rMessage").remove();
                } else {
                    alert("评论失败！");
                }
            }, error: function (err) {
                console.log(err);
            }
        });
}

//请求热门评论数据
function getHotComment() {
    var musicRid = $("#musicRid").val();
    $("#recList").load("/comment/selectHotComment",{musicRid:musicRid});
}

//请求最新评论数据
function getNewetsComment(currentPage) {
    var musicRid = $("#musicRid").val();
    $("#newest").load("/comment/selectNewestComment",{musicRid:musicRid,currentPage:currentPage});
}

//删除评论
function deleteComment(self,cId) {
    $(".popupWindow").show();
    $(".confirm").click(function () {
        $(self).parent().parent().parent().remove();
        $.ajax({
            url:"/comment/deleteComment",
            type:"post",
            data:{cId:cId},
            success:function (result) {
                $(".popupWindow").hide();
                console.log(result);
            },error:function (err) {
                console.log(err);
            }
        });
    });

}

//点赞
function clickZan(self,cId,uId) {
    if (!isLogin()) {
        var isZan = 0;//判断是否是点赞,默认是
        if ($(self).hasClass("praised")) {
            $(self).removeClass("praised");
            var likeNum = $(self).text();
            $(self).text("" + (parseInt(likeNum) - 1) + "");
            isZan = 1;
        } else {
            $(self).addClass("praised");
            var likeNum = $(self).text();
            $(self).text("" + (parseInt(likeNum) + 1) + "");
            isZan = 0;
        }
        //添加用户和评论之间的点赞关系
        $.ajax({
            url: "/comment/insertVotelog",
            type: "post",
            data: {cId: cId, uId: uId, isZan: isZan},
            success: function (result) {
                console.log(result);
            }, error: function (err) {
                console.log(err);
            }
        });
        //修改点赞或取消赞
        $.ajax({
            url: "/comment/clickZan",
            type: "post",
            data: {cId: cId, uId: uId, isZan: isZan},
            success: function (result) {
                console.log(result);
            }, error: function (err) {
                console.log(err);
            }
        });

    }
}